/*
 * DPI C functions for simple_dpi test.
 * These are just transparent wrappers around SV TF calls.
 */

#include "vpi_user.h"
#include "main.h"
//#include "driver.h"
svScope my_scope;

void LoadPacketList()
{
}
void StorePacketList()
{
}

void DisplayPacket(int ptrPacket, int Length)
{
  int Packet[1024];
  int i;
  MemRead(ptrPacket, Length/4, Packet);
  for(i=0; i<Length/4; i++)
    printf("0x%08X ", Packet[i]);
  printf("\n");
  return;
}

void DisplayPacketList(int ptrFirstDescriptor)
{
  int Data[1024];
  int ptrDescriptor;
  int ptrPacket;
  int PacketLength;
  int PacketCount;
  ptrDescriptor = ptrFirstDescriptor;
  PacketCount = 0;
  do
  {
    MemRead(ptrDescriptor, 4, Data);
    ptrPacket = Data[0];
    PacketLength = Data[2] & 0xFFFF;
    printf("0x%08X: ", ptrPacket);
    DisplayPacket(ptrPacket, PacketLength);
    ptrDescriptor = Data[1];
    PacketCount++;
  } while ((ptrDescriptor != 0) && (PacketCount<20));
  printf("Num Packets in the List: %d\n", PacketCount);
}

void TestMemWrite()
{
  int Data[1024];
  int i;
  for (i=0; i<100; i++)
    Data[i] = i;
  MemWrite(0x0, 100, Data);
}
int Test_axis_recorder()
{
  int PacketCount;
  int i;
  int State;
  int CurrentDescriptor;
  RegWriteDWord(0x24, 0x0);
  RegWriteDWord(0x2C, 0x1000);
  RegWriteByte(0x20, 0x1);
  PacketCount = 0;
  while(PacketCount != 10)
    RegReadDWord(0x34, &PacketCount);
  RegWriteByte(0x20, 0x0);
  
  do
  {
    RegReadDWord(0x20, &State);
    State = (State & 0x000F0000) >> 16;
  }while(State !=0);
  
  RegReadDWord(0x28, &CurrentDescriptor);
  DisplayPacketList(CurrentDescriptor);
  TestMemWrite();
  return 0;
}

DPI_DLLESPEC
int StartSoftware()
{
  int rd_data;
  printf("Starting Software\n");
//  my_scope = svGetScope();
//  printf("My Scope = %s\n", svGetNameFromScope(my_scope));
//  svSetScope(my_scope);
//  printf("Before Calling TestTask\n");
//  TestTask(15);
//  printf("After Calling TestTask\n");
//  svSetScope(svGetScope());
  RegWriteByte(0x1, 0xab);
  RegWriteWord(0x2, 0xabcd);
  RegWriteDWord(0x24, 0x01234567);
  RegReadDWord(0x24, &rd_data);
  printf("data read = 0x%08x", rd_data);
  Test_axis_recorder();
    
  return 0;
}
